package nuist.hanniko.stu.webbackend.controller.api;

import com.alibaba.fastjson.JSONObject;
import nuist.hanniko.stu.webbackend.model.LwM2m.request.ExecuteRequest;
import nuist.hanniko.stu.webbackend.model.LwM2m.request.ReadRequest;
import nuist.hanniko.stu.webbackend.model.LwM2m.request.WriteRequest;
import nuist.hanniko.stu.webbackend.service.cmd_runner.DeviceRequestService;
import nuist.hanniko.stu.webbackend.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/device/request")
public class DeviceRequestController {

    private final DeviceRequestService deviceRequestService;

    @Autowired
    public DeviceRequestController(DeviceRequestService deviceRequestService) {
        this.deviceRequestService = deviceRequestService;
    }

    @PostMapping("read")
    @ResponseBody
    public ResponseData read(@RequestParam("deviceId") String deviceId,
                             @RequestParam("objectId") String objectId,
                             @RequestParam(value = "resourceId", required = false) String resourceId) {
        ResponseData responseData = new ResponseData();

        ReadRequest readRequest = new ReadRequest(deviceId, objectId, resourceId);
        JSONObject resp = deviceRequestService.sendRequest(readRequest);

        if (resp == null || resp.containsKey("err")) {
            responseData.setStatus(false);
            responseData.setMessage("error");
        }
        responseData.setData(resp != null ? resp.get("rep") : null);

        return responseData;
    }

    @PostMapping("write")
    @ResponseBody
    public ResponseData write(@RequestParam("deviceId") String deviceId,
                              @RequestParam("objectId") String objectId,
                              @RequestParam("resourceId") String resourceId,
                              @RequestParam("value") String value,
                              @RequestParam("nodeKind") String nodeKind) {
        ResponseData responseData = new ResponseData();

        WriteRequest request = new WriteRequest(deviceId, objectId, resourceId, nodeKind, value);
        JSONObject resp = deviceRequestService.sendRequest(request);

        if (resp == null || resp.containsKey("err")) {
            responseData.setStatus(false);
            responseData.setMessage("error");
        }
        responseData.setData(resp);

        return responseData;
    }

    @PostMapping("execute")
    @ResponseBody
    public ResponseData execute(@RequestParam("deviceId") String deviceId,
                                @RequestParam("objectId") String objectId,
                                @RequestParam("resourceId") String resourceId,
                                @RequestParam("parameters") String parameter) {
        ResponseData responseData = new ResponseData();

        ExecuteRequest request = new ExecuteRequest(deviceId, objectId, resourceId, parameter);
        JSONObject resp = deviceRequestService.sendRequest(request);

        if (resp == null || resp.containsKey("err")) {
            responseData.setStatus(false);
            responseData.setMessage("error");
        }
        responseData.setData(resp);

        return responseData;
    }

    @PostMapping("observe")
    @ResponseBody
    public ResponseData observe(@RequestParam("deviceId") String deviceId,
                                @RequestParam("objectId") String objectId,
                                @RequestParam(value = "resourceId", required = false) String resourceId) {
        ResponseData responseData = new ResponseData();
        // todo: webSocket

        return responseData;
    }

    @PostMapping("cancelObserve")
    @ResponseBody
    public ResponseData cancelObserve() {
        ResponseData responseData = new ResponseData();
        // todo: webSocket

        return responseData;
    }
}
